package com.pzb.goods.service.impl;

import java.util.*;

import com.alibaba.fastjson.JSONObject;
import com.pzb.common.core.domain.entity.GdGoodsClass;
import com.pzb.common.utils.StringUtils;
import com.pzb.goods.domain.GdGoods;
import com.pzb.goods.domain.GdGoodsUnits;
import com.pzb.goods.domain.GoodsPicture;
import com.pzb.goods.mapper.GdGoodsClassMapper;
import com.pzb.goods.mapper.GdGoodsMapper;
import com.pzb.goods.mapper.GdGoodsUnitsMapper;
import com.pzb.goods.service.IGdGoodsService;
import com.pzb.market.mapper.MkPanicbuyingMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 商品管理Service业务层处理
 * 
 * @author Mr_Wu
 * @date 2021-11-30
 */
@Service
public class GdGoodsServiceImpl implements IGdGoodsService
{
    @Autowired
    private GdGoodsClassMapper gdGoodsClassMapper;
    @Autowired
    private GdGoodsMapper gdGoodsMapper;
    @Autowired
    private GdGoodsUnitsMapper goodsUnitsMapper;
    @Autowired
    private MkPanicbuyingMapper panicbuyingMapper;


    @Override
    public List<GdGoodsClass> selectGdGoodsClassList(String extAppid) {
        List<GdGoodsClass> classList = gdGoodsClassMapper.selectGdGoodsClassList(extAppid);
        return buildGoodsClassTree(classList);
    }

    @Override
    public List<GdGoods> selectGdGoodsList(String extAppid, Long classId,String name,String order) {
        return gdGoodsMapper.selectGdGoodsList(extAppid,classId,name,order);
    }


    /**
     * 查询商品管理
     *
     * @param id 商品管理主键
     * @return 商品管理
     */
    @Override
    public GdGoods selectGdGoodsById(Long id,String extAppid,String provinceCode)
    {
        GdGoods goods = gdGoodsMapper.selectGdGoodsById(id,extAppid,provinceCode);
        //查询商品图片
        List<GoodsPicture> pictureList = gdGoodsMapper.selectPictureListByGoodsId(id);
        goods.setPictureList(pictureList);
        //查询商品规格
        List<GdGoodsUnits> unitsList = goodsUnitsMapper.selectGdGoodsUnitsList(id);
        goods.setUnitsList(unitsList);
        //组织规格
        Set<String > keys = null;
        List<String> unitKeys=new ArrayList<>();
        List<List<JSONObject>> goodsUnit = new ArrayList<>();
        if(unitsList!=null && unitsList.size()>1){
            for (GdGoodsUnits gdGoodsUnits : unitsList) {
                JSONObject unit = JSONObject.parseObject(gdGoodsUnits.getGoodsUnit());
                keys = unit.keySet();
                break;
            }
            for (String key : keys) {
                unitKeys.add(key);
                List<JSONObject> set = new ArrayList<>();
                for (GdGoodsUnits gdGoodsUnits : unitsList) {
                    JSONObject unit = JSONObject.parseObject(gdGoodsUnits.getGoodsUnit());
                    JSONObject a = new JSONObject();
                    a.put("name",unit.getString(key));
                    a.put("ishow",true);
                    if(!set.contains(a))
                    set.add(a);
                }

                goodsUnit.add(set);
            }
            goods.setUnitKeys(unitKeys);
            goods.setGoodsUnit(goodsUnit);
        }


        //查询商品属性
       /* List<GdGoodsAttributeValue> attributeValueList = goodsAttributeValueService.selectGdGoodsAttributeValueListByGoodsId(id);
        goods.setAttributeValueList(attributeValueList);*/
        return goods;
    }

    @Override
    public GdGoodsUnits selectGoodsUnitsById(Long goodsUnitId) {
        return goodsUnitsMapper.selectGoodsUnitsById(goodsUnitId);
    }

    /**
     * 修改规格属性
     *
     * @param gdGoodsUnits 规格属性
     * @return 结果
     */
    @Override
    public int updateGdGoodsUnits(GdGoodsUnits gdGoodsUnits) {
        return goodsUnitsMapper.updateGdGoodsUnits(gdGoodsUnits);
    }

    @Override
    public GdGoodsUnits selectGoodsUnitsByGoodsIdAndUnits(Long goodsId, String goodsUnit) {
        return goodsUnitsMapper.selectGoodsUnitsByGoodsIdAndUnits(goodsId,goodsUnit);
    }

    @Override
    public List<GdGoods> selectGiftListByDiscountId(Long discountId) {
        return gdGoodsMapper.selectGiftListByDiscountId(discountId);
    }

    @Override
    public List<GdGoods> selectPanicGoodsList(Long panicId) {
        return gdGoodsMapper.selectPanicGoodsList(panicId);
    }


    /**
     * 构建前端所需要树结构
     *
     * @param goodsClasses 机构列表
     * @return 树结构列表
     */
    public List<GdGoodsClass> buildGoodsClassTree(List<GdGoodsClass> goodsClasses)
    {
        List<GdGoodsClass> returnList = new ArrayList<GdGoodsClass>();
        List<Long> tempList = new ArrayList<Long>();
        for (GdGoodsClass goodsClass : goodsClasses)
        {
            tempList.add(goodsClass.getId());
        }
        for (Iterator<GdGoodsClass> iterator = goodsClasses.iterator(); iterator.hasNext();)
        {
            GdGoodsClass goodsClass = (GdGoodsClass) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(goodsClass.getParentId()))
            {
                recursionFn(goodsClasses, goodsClass);
                returnList.add(goodsClass);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = goodsClasses;
        }
        return returnList;
    }
    /**
     * 递归列表
     */
    private void recursionFn(List<GdGoodsClass> list, GdGoodsClass t)
    {
        // 得到子节点列表
        List<GdGoodsClass> childList = getChildList(list, t);
        t.setChildren(childList);
        for (GdGoodsClass tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }
    /**
     * 得到子节点列表
     */
    private List<GdGoodsClass> getChildList(List<GdGoodsClass> list, GdGoodsClass t)
    {
        List<GdGoodsClass> tlist = new ArrayList<GdGoodsClass>();
        Iterator<GdGoodsClass> it = list.iterator();
        while (it.hasNext())
        {
            GdGoodsClass n = (GdGoodsClass) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<GdGoodsClass> list, GdGoodsClass t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }

}
